package com.zkj.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zkj.dto.SetmealDTO;
import com.zkj.dto.SetmealPageQueryDTO;
import com.zkj.entity.Setmeal;
import com.zkj.entity.SetmealDish;
import com.zkj.mapper.SetmealDishMapper;
import com.zkj.result.PageResult;
import com.zkj.result.Result;
import com.zkj.service.ISetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 套餐 前端控制器
 * </p>
 *
 * @author zkj
 * @since 2024-08-22
 */

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
@Slf4j
public class SetmealController {
    @Autowired
    private ISetmealService setmealService;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result page(SetmealPageQueryDTO setmealPageQueryDTO) {
        Page<Setmeal> page = new Page<>(setmealPageQueryDTO.getPage(),
                setmealPageQueryDTO.getPageSize());
        return setmealService.pageQuery(page, setmealPageQueryDTO);
    }

    /**
     * 新增套餐
     *
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增套餐")
    @Transactional
    @CacheEvict(cacheNames = "setmealCache", key = "#setmealDTO.categoryId")//key: setmealCache::100
    public Result save(@RequestBody SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealService.save(setmeal);
        Long id = setmeal.getId();
        if (setmealDTO.getSetmealDishes() != null && setmealDTO.getSetmealDishes().size() > 0) {
            for (SetmealDish setmealDish : setmealDTO.getSetmealDishes()) {
                setmealDish.setSetmealId(id);
            }
            setmealDishMapper.insertBatch(setmealDTO.getSetmealDishes());
        }
        return Result.success();
    }


    @DeleteMapping
    @ApiOperation("批量删除套餐")
    @Transactional
    public Result delete(@RequestParam List<Long> ids) {
        setmealService.removeByIds(ids);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("停售/起售套餐")
    @Transactional
    public Result updateStatus(@PathVariable Integer status, @RequestParam Long id) {

        Setmeal setmeal = setmealService.getById(id);
        if(setmeal == null) {
            return Result.error("套餐不存在");
        }
        setmeal.setStatus(status);
        setmealService.updateById(setmeal);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result get(@PathVariable Long id) {
        Setmeal setmeal = setmealService.getById(id);
        SetmealDTO setmealDTO = new SetmealDTO();
        BeanUtils.copyProperties(setmeal, setmealDTO);
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(queryWrapper);
        setmealDTO.setSetmealDishes(setmealDishes);
        return Result.success(setmealDTO);
    }

    @PutMapping
    @ApiOperation("修改套餐")
    @Transactional
    public Result update(@RequestBody SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealService.updateById(setmeal);
        Long id = setmeal.getId();
        log.info("id: " + id);
        setmealDishMapper.delete(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId,
                id));
        if (setmealDTO.getSetmealDishes() != null && setmealDTO.getSetmealDishes().size() > 0){
            for (SetmealDish setmealDish : setmealDTO.getSetmealDishes()) {
                setmealDish.setSetmealId(id);
            }
            setmealDishMapper.insertBatch(setmealDTO.getSetmealDishes());
        }
        return Result.success();
    }
}

